package com.moli.iov.task.jobhandler;

import com.moli.iov.alarm.remote.IAlarmQuantityStatRemoteService;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.DateUtil;
import com.moli.iov.util.StringUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * <p>
 * Manual trigger synchronized alarm quantity statistics job.
 * </p>
 *
 * @author OAK
 * @version 3.0
 * @since 2019/10/21 10:39 AM.
 */
@Slf4j
@JobHander(value="manualTriggerAlarmQuantityStatJobHandler")
@Component
public class ManualTriggerAlarmQuantityStatJobHandler extends IJobHandler {

    @Autowired
    IAlarmQuantityStatRemoteService alarmQuantityStatRemoteService;

    @Override
    public ReturnT<String> execute(String... param) throws Exception {
        try {
            if(StringUtil.isBlank(param[0])){
                throw new BusinessException("Not found manual trigger to synchronized alarm quantity statistics in parameter.");
            }
            int i = Integer.parseInt(param[0]) + 1;
            log.info(" Execute manual trigger to synchronized alarm quantity statistics in parameter : {}", param);
            while(i > 0) {
                final String currentDate = DateUtil.format(DateUtil.YEAR_DAY,
                        new Date(Instant.now().minus(--i, ChronoUnit.DAYS).toEpochMilli()));
                RestResponse<String> restResponse = alarmQuantityStatRemoteService.executeGenericAlarmQuantityStat(currentDate);
            }
            log.info(" Execute manual trigger to synchronized alarm quantity statistics success.");
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.error(" Execute manual trigger to synchronized alarm quantity statistics found a fail, {}", e);
            return ReturnT.FAIL;
        }
    }
}
